/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility;

/**
 *
 * @author suzunghia
 */
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ColorPane extends JTextPane {

    private String[] KeyWords = {"select", "insert", "update", "delete", "from", "where", 
        "order", "group","by","value", "into", "values", "set","as","on","left",
        "right","join","and", "or", "having", "create", "table"};
    private String[] KeyExpressions = {"!=", ">=", "<=", "=", "<>", ">", "<", "not like", "like","max","min","sum","avg","count"};

    public void appendNaive(Color c, String s) { // naive implementation
        // bad: instiantiates a new AttributeSet object on each call
        SimpleAttributeSet aset = new SimpleAttributeSet();
        StyleConstants.setForeground(aset, c);

        int len = getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret     
    }

    public void append(Color c, String s) { // better implementation--uses
        // StyleContext
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);

        int len = getDocument().getLength(); // same value as
        // getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public void EditorQueryKeyPressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        if (evt.getKeyCode() == 32) {
            String str = this.getText();
            this.setText("");
            String[] sub = str.split("\r\n");
            for (int i = 0; i < sub.length; i++) {
                ProcessTextinLine(sub[i]);
                if (sub.length > 1 && i < sub.length - 1) {
                    this.append(Color.red, "\r\n");
                }
            }
        }
    }

    private void ProcessTextinLine(String str) {
        String[] sub = str.split(" ");
        for (int i = 0; i < sub.length; i++) {
            if (CheckKeyWord(sub[i])) {
                this.append(Color.BLUE, sub[i]);
            } else if (CheckKeyExpression(sub[i])) {
                this.append(Color.red, sub[i]);
            } else {
                this.append(Color.BLACK, sub[i]);

            }
            this.append(Color.BLACK, " ");
        }

    }

    private boolean CheckKeyWord(String str) {
        for (int i = 0; i < KeyWords.length; i++) {
            if (str.equalsIgnoreCase(KeyWords[i])) {
                return true;
            }
        }
        return false;
    }

    private boolean CheckKeyExpression(String str) {
        for (int i = 0; i < KeyExpressions.length; i++) {
            if (str.equalsIgnoreCase(KeyExpressions[i])) {
                return true;
            }
        }
        return false;
    }
}
